<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;

class Controller extends BaseController
{
  use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
  
  protected function denialUser()
  {
    return view('application.info', [
      'title' => '拒绝访问',
      'icon' => 'warn',
      'content' => '账户已被封禁'
    ]);
  }

  public function upload()
  {
    $file = request()->file('file');
    
    if (isset($file) && $file->isValid()) {
        //获取文件的扩展名
        $ext = $file->getClientOriginalExtension();
        //获取文件的绝对路径
        $path = $file->getRealPath();
        //定义文件名
        $filename = time() . str_random(5) . '.' . $ext;
        //存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
        if (Storage::disk('public')->put($filename, file_get_contents($path))) {
            return response()->json(['code' => 0, 'msg' => '上传成功', 'data' => ['src' => env('APP_URL') . '/storage/app/public/' . $filename, 'name' => $filename]]);
        }
    }
    return response()->json(['code' => 1, 'msg' => '上传失败', 'data' => ['src' => '']]);
  }
}
